package com.ys.utils.yinsheng.yztdock.util;

import java.util.Collection;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ObjectHelper {

    private static Random random = new Random();

    /**
     * 判断给定对象是否为空或者内容是否为空
     * null     =====>  true
     * ""       =====>  true
     * "    "   =====>  true
     * {}       =====>  true
     * []       =====>  true
     * others   =====>  false
     */
    public static boolean isEmpty(Object obj) {
        if (obj == null) {
            return true;
        }
        if (obj instanceof Collection) {
            return ((Collection<?>) obj).isEmpty();
        }

        if (obj instanceof String) {
            return ((String) obj).equalsIgnoreCase("null")
                    | ((String) obj).trim().toString().equals("");
        }

        if (obj instanceof StringBuffer) {
            return ((StringBuffer) obj).length() == 0;
        }

        if (obj.getClass().isArray()) {
            try {
                Object[] a = (Object[]) obj;

                boolean b = true;
                for (Object o : a) {
                    b = b & isEmpty(o);

                    if (!b) {
                        break;
                    }
                }

                return b;
            } catch (ClassCastException e) {
            }
        }
        return false;
    }

    /**
     * 判断这个Object是否不为Null或长度不为0
     */
    public static boolean isNotEmpty(Object obj) {
        return !isEmpty(obj);
    }

    /**
     * 生成指定长度的包含大小写字母数字的随机码
     */
    public static String getRandomCode(int length) {
        StringBuilder codeNum = new StringBuilder();
        int[] code = new int[3];
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            int num = random.nextInt(10) + 48;
            int uppercase = random.nextInt(26) + 65;
            int lowercase = random.nextInt(26) + 97;
            code[0] = num;
            code[1] = uppercase;
            code[2] = lowercase;
            codeNum.append((char) code[random.nextInt(3)]);
        }
        return codeNum.toString();
    }

    /**
     * 下划线转驼峰命名
     */
    public static String lineToHump(String line) {
        Pattern linePattern = Pattern.compile("_(\\w)");
        line = line.toLowerCase();
        Matcher matcher = linePattern.matcher(line);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /**
     * 驼峰转下划线
     */
    public static String humpToLine(String hump) {
        Pattern humpPattern = Pattern.compile("[A-Z]");
        Matcher matcher = humpPattern.matcher(hump);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    public static String humpToPascal(String hump) {
        return hump.substring(0, 1).toUpperCase() + hump.substring(1);
    }

    /**
     * 获取指定范围的随机整数
     */
    public static int getRandom(int min, int max) {
        int i = min;
        try {
            i = random.nextInt(max) % (max - min + 1) + min;
        } catch (Exception ignored) {
        }
        return i;
    }

    public static boolean isNumeric(String str){
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }
}
